import Vue from 'vue'
// import Router from 'vue-router'
import VueRouter from 'vue-router';
const Home = () => import('../views/home/Home')
const Category = () => import('../views/category/Category')
const Cart = () => import('../views/cart/Cart')
const Profile = () => import('../views/profile/Profile')
Vue.use(VueRouter)
const routes = [{
    path: '',
    redirect: '/home',

  }, {
    path: '/home',
    component: Home,
    meta: {
      title: '首页',
      description:'首页555'
    }
  },
  {
    path: '/category',
    component: Category,
    meta: {
      title: '分类',
      description:'分类555'
    }
  },
  {
    path: '/cart',
    component: Cart,
    meta: {
      title: '购物车',
      description:'购物车1111'
    }
  },
  {
    path: '/profile',
    component: Profile,
    meta: {
      title: '我的',
      description:'我的111'
    }
  }
];
const router = new VueRouter({
  routes,
  mode: 'history'
})
router.beforeEach((to, from, next) => {
  // document.title=to.meta.title
  document.title = to.matched[0].meta.title
  // document.meta[0].des = to.matched[0].meta.title
  // var dMeta = document.createElement('meta');
  // dMeta.setAttribute('name', to.matched[0].meta.title);
  //       dMeta.setAttribute('content', to.matched[0].meta.title);
  
 const meta= document.head.getElementsByTagName('meta');
// console.log(meta.length);
// var i
for( let i in meta){
 if(typeof meta[i].name!="undefined"&&meta[i].name.toLowerCase()=="description"){
   meta[i].content=to.matched[0].meta.description;
 }
 if(typeof meta[i].name!="undefined"&&meta[i].name.toLowerCase()=="keywords"){
  meta[i].content=to.matched[0].meta.description+','+to.matched[0].meta.title;
}
}

next();
})
this.$route.path
router.afterEach((to,from)=>{
  console.log(to)
})
// export default new Router({
//   routes: [
//     // {
//     //   path: '/', 
//     //   name: 'HelloWorld',
//     //   component: HelloWorld
//     // }
//   ]
// })
export default router
